/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.helpers.becario;

import com.uca.sisbec.mapping.Becario;
import com.uca.sisbec.mapping.Universidad;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hibernate.Query;
import org.hibernate.Session;

public class IrregularidadHelper {
    private Session session;
    
    public IrregularidadHelper(Session session){
        this.session = session;
    }

    public boolean isValidCarnet(Becario becario) {
        Universidad u = (Universidad) session.get(Universidad.class, becario.getUniversidad().getIdUniv());
        String pattern = u.getFormatoCarnet();
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(becario.getCarnet());
        return m.find();
    }

    public String getCumDeBecario(Becario becario) {
        String hql = "select str(sum(mb.notaFinal * m.uv) / sum(m.uv)) "
                + "from MateriaXBecario mb, Materia m "
                + "where mb.id.codMateria = m.codMateria "
                + "and mb.id.carnet = :v_carnet ";
        Query query = session.createQuery(hql);
        query.setString("v_carnet", becario.getCarnet());
        return (String) query.uniqueResult();
    }

    public boolean isPendienteLibros(Becario becario) {
        String hql = "select 'Y' "
                + "from Prestamo "
                + "where id.carnet = :v_carnet "
                + "and fecDevolucion is null ";
        Query query = session.createQuery(hql);
        query.setString("v_carnet", becario.getCarnet());
        List<String> list = new ArrayList<String>(query.list());
        if (!(list == null || list.isEmpty()))
            return true;
        else
            return false;
    }
    
    
}
